/*

 Copyright (C) 2009 Team16
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. This program is distributed in the
hope that it will be useful, but WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License
for more details. You should have received a copy of the GNU General
Public License along with this program. If not, see <http://www.gnu.org/licens

  */
#include <QtGui>
#include "config.h"
#include <QMessageBox>

config::config(QWidget *parent)
    : QDialog(parent)
{
    save=new QPushButton(tr("Save"),this);
    checkbox = new QCheckBox("v-QMake\\o-TMake", this);

QString gs;
    QFile file(qApp->applicationDirPath()+"/data/config.dat");
        if (!file.open(QFile::ReadOnly | QFile::Text)) {
            QMessageBox::warning(this, tr("Options"),
                                 tr("Cannot read file %1:\n%2")
                                 .arg("data/config.dat")
                                 .arg(file.errorString()));
           gs="";
        }else{


        QTextStream in(&file);
        gs=in.readLine(256);
        QString hs=in.readLine(256);
        if (hs=="qmake")checkbox ->setChecked(true);
    }

file.close();


    lay=new QHBoxLayout(this);
    this->setLayout(lay);
    pth=new QLabel(tr("Path to QT(without ending \\):\n(for ex:I:\\Qt\\2009.03):"),this);
    qtpath=new QLineEdit(gs,this);

    qtpath->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Minimum);
    this->resize(640,40);


    lay->addWidget(pth);
    lay->addWidget(qtpath);
    lay->addWidget(save);
    lay->addWidget(checkbox);
this->setWindowTitle(tr("Options"));

connect(save,SIGNAL(clicked()),this ,SLOT(save_click()));

}

void config::save_click(){
     QFile file(qApp->applicationDirPath()+"/data/config.dat");
        if (!file.open(QFile::WriteOnly | QFile::Text)) {
            QMessageBox::warning(this, tr("Options"),
                                 tr("Cannot write file %1:\n%2")
                                 .arg("data/config.dat")
                                 .arg(file.errorString()));
            return;
        }

        if(!QFile(qtpath->text()).exists()){
             QMessageBox::warning(this, tr("Options"),
                                 tr("File not found"));
            return;

        }
        QTextStream out(&file);
        out<<qtpath->text()<<"\n";
        if (checkbox->isChecked()){
            out<<"qmake";
        }else{
            out<<"tmake";
        }
        file.close();
this->close();

QMessageBox::StandardButton reply;
                reply = QMessageBox::information(this, tr("To apply..."),
                tr("To apply new settings you must restart programm"),
                QMessageBox::Ok);

qApp->exit(0);
}
